package com.huike.contract.service.impl;

import com.huike.business.domain.TbBusiness;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.TbCourse;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.clues.mapper.TbCourseMapper;
import com.huike.common.exception.CustomException;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.contract.domain.TbContract;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.contract.service.TbContractService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 合同 服务实现类
 * </p>
 *
 * @author Richard
 * @since 2023-12-12
 */
@Service
public class TbContractServiceImpl implements TbContractService {
    @Resource
    private TbContractMapper tbContractMapper;

    @Resource
    private TbCourseMapper courseMapper;

    @Resource
    private TbActivityMapper activityMapper;

    @Resource
    private TbBusinessMapper tbBusinessMapper;

    @Override
    public List<TbContract> selectTbContractList(TbContract tbContract) {
        // 根据TbContract对象查询TbContract列表
        return tbContractMapper.selectTbContractList(tbContract);
    }

    @Override
    public int insertTbContract(TbContract tbContract) {
        //生成id
        String id = String.valueOf(System.currentTimeMillis());
        tbContract.setId(id);
        //设置创建者
        tbContract.setCreateBy(SecurityUtils.getUsername());
        //设置创建时间
        tbContract.setCreateTime(DateUtils.getNowDate());
        //设置部门id
        tbContract.setDeptId(SecurityUtils.getDeptId());
        //设置状态
        tbContract.setStatus("4");//状态(待审核1，已通过2，已驳回3 全部完成4)
        //设置订单
        setOrder(tbContract);
        //插入合同
        return tbContractMapper.insertTbContract(tbContract);
    }

    @Override
    public TbContract selectTbContractById(Long id) {
        //根据id查询合同
        return tbContractMapper.selectTbContractById(id);
    }

    @Override
    public int changeContract(Long businessId, TbContract tbContract) {
        //根据id查询商机
        TbBusiness tbBusiness= tbBusinessMapper.selectTbBusinessById(businessId);
        //如果商机里面没有选择课程，则抛出异常
        if(tbBusiness.getCourseId()==null){
            throw new CustomException("商机里面没有选择课程，无法转换客户合同");
        }

        //将商机状态改为转换客户
        tbBusiness.setStatus(TbBusiness.StatusType.TOCUSTOMER.getValue());
        tbBusinessMapper.updateTbBusiness(tbBusiness);

        //设置合同创建时间
        tbContract.setCreateTime(DateUtils.getNowDate());
        //设置商机id
        tbContract.setBusinessId(businessId);
        //将商机属性复制到合同
        BeanUtils.copyProperties(tbBusiness, tbContract);
        //插入合同
        return  insertTbContract(tbContract);
    }

    @Override
    public int updateTbContract(TbContract tbContract) {
        //将合同状态改为已通过
        tbContract.setStatus("2");
        //设置订单
        setOrder(tbContract);
        //更新合同
        return tbContractMapper.updateTbContract(tbContract);
    }

    private void setOrder(TbContract tbContract){
        //根据id查询课程
        TbCourse course= courseMapper.selectTbCourseById(tbContract.getCourseId());
        //给合同设置课程价格
        tbContract.setCoursePrice(course.getPrice());
        //根据id查询活动
        TbActivity tbActivity= activityMapper.selectTbActivityById(tbContract.getActivityId());
        //如果活动不为空
        if(tbActivity!=null){
            //如果活动类型为1则有折扣,无代金券
            if("1".equals(tbActivity.getType())){
                //设置折扣类型
                tbContract.setDiscountType("课程折扣");
                //计算订单
                BigDecimal price = new BigDecimal(course.getPrice());
                BigDecimal disCount = new BigDecimal(tbActivity.getDiscount());
                BigDecimal order = price.multiply(disCount.divide(new BigDecimal(10),2,BigDecimal.ROUND_CEILING));
                tbContract.setOrder(order.floatValue());
//                tbContract.setOrder(course.getPrice()*tbActivity.getDiscount());
            }else{//活动类型为2则有代金券,无折扣
                //当代金券大于等于合同50% 则按照原价,否则减去代金券金额
                //如果代金券大于等于原课程50%
                if(tbActivity.getVouchers()>=course.getPrice()/2){
                    //按照原价
                    tbContract.setOrder(course.getPrice());
                    //设置折扣类型
                    tbContract.setDiscountType("代金券大于原课程50%,代金券不可用");
                }else{
                    //按照原价减去代金券
                    tbContract.setDiscountType("代金券");
                    tbContract.setOrder(course.getPrice()-tbActivity.getVouchers());
                }
            }
        }else{
            //如果没有活动，则按照原价
            tbContract.setDiscountType("无");
            tbContract.setOrder(course.getPrice());
        }
    }
}
